<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
	<!--通用的header-->
	<jsp:include page="/views/includes/base_header.jsp">
		<jsp:param value="代码生成(单表)" name="title"/>
	</jsp:include>
	
	<body>
		<!--导航条-->
		<jsp:include page="/views/includes/top_navbar.jsp"></jsp:include>
		
		<div class="container-fluid">
			<div class="row">
				<div class="col-md-2">
					<!--菜单栏-->		
					<jsp:include page="/views/includes/left_menu.jsp">
						<jsp:param value="dept_manage" name="active"/>
					</jsp:include>
				</div>
				<div class="col-md-9">
					<div class="panel panel-primary">
						<div class="panel-heading">
							代码生成表单(单表)
						</div>
						<div class="panel-body">
							<form  class="form-horizontal"  role="form" onsubmit="return false;">
								<!-- 
								{
								"moduleInfo":"角色",
								"className":"Role",
								"tableName":"test_role",
								"listField":[
									{
										"fieldName":"roleName",
										"fieldType":"String",
										"fieldInfo":"角色名"
									},
									{
										"fieldName":"roleInfo",
										"fieldType":"String",
										"fieldInfo":"角色描述"
									}
								 ]
							    }
								 -->
							
								
								<!-- 模块基本信息 -->
								<div class="panel panel-default">
								    <div class="panel-heading">
								        <h3 class="panel-title">
								       	 模块基本信息
								        </h3>
								    </div>
								    <div class="panel-body">
								  	  
								  	  <label for="moduleInfo" class="col-sm-1 control-label">模块名称</label>
										<div class="col-sm-3">
											<input  name="moduleInfo"  type="text" 
												   class="form-control" 
												   placeholder="请输入（用户）"
												   >
										</div>
										
										<label for="className" class="col-sm-1 control-label">实体类名</label>
										<div class="col-sm-3">
											<input  name="className"  type="text" 
												   class="form-control" 
												   placeholder="请输入（User）"
												   >
										</div>
										
										<label for="tableName" class="col-sm-1 control-label">数据表名</label>
										<div class="col-sm-3">
											<input  name="tableName"  type="text" 
												   class="form-control" 
												   placeholder="请输入（test_user）"
												   >
										</div>
								    </div>
								</div>
								
								<!--模块属性信息-->
								<div class="panel panel-default">
								    <div class="panel-heading">
								        <h3 class="panel-title">
								       	 模块属性信息(类的属性)
								        </h3>
								    </div>
								    
								    <!-- 装多个属性的div -->
								    <div class="panel-body">
								  	  	
								  	   <!-- 单个属性 -->
								  	   <div name='fieldDiv' class="form-group ">
								  	   		<div class=" col-sm-1">
								  	   			<button onclick="addField()" class="btn btn-info">添加</button>
								  	   			<button onclick="delField(this)" class="btn btn-deafult">删除</button>
								  	   		</div>
								  	   		<label for="fieldInfo" class="col-sm-1 control-label">属性描述</label>
											<div class="col-sm-2">
												<input name="fieldInfo"  type="text" 
													   class="form-control" 
													   placeholder="请输入（用户账号）"
													   >
											</div>
											<label for="fieldName" class="col-sm-1 control-label">属性名称</label>
											<div class="col-sm-2">
												<input name="fieldName"  type="text" 
													   class="form-control" 
													   placeholder="请输入（userName）"
													   value="${user.password}"
													   >
											</div>
											<label for="password" class="col-sm-1 control-label">属性类型</label>
											<div class="col-sm-3">
												<div class="radio">
													<label> <input type="radio" name="fieldType"  value="String" checked>String</label>
												    <label> <input type="radio" name="fieldType"  value="Integer" >Integer</label>
												    <label> <input type="radio" name="fieldType"  value="Date" >Date</label>
												</div>
											</div>
										</div>
								  	  
								    </div>
								</div>
							
								
								
									
								<!-- 按钮 -->
								<div class="form-group">
									<div class="col-sm-offset-2 col-sm-10">
										
										<button onclick="saveData()" class="btn btn-success">保存</button>
										<button type="reset"  class="btn btn-info">重置</button>
									</div>
								</div>
								
								
							 </form>
							
						</div>
						
					</div>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
	
	/**
		动态删除属性表单
	*/
	function delField(dom){
		var fieldDiv= $(dom).parent().parent();
		fieldDiv.remove();
	}
	/**
		动态添加属性表单
	*/
	function addField(){
		debugger;
		//找到所有属性div
		var fieldDivArr= $("div[name='fieldDiv']");
		//最后一个
		var fieldDiv=fieldDivArr.get(fieldDivArr.size()-1);
		
		var oldFieldDivHtml=fieldDiv.outerHTML;
		oldFieldDivHtml=oldFieldDivHtml.replace(/fieldType[^"]*/g,'fieldType'+Math.random());
		//把自己的单选框的name调整下
		var radio= $(fieldDiv).find('input[type="radio"]');
		radio.attr('name',radio.attr('name')+1);
		//父div==添加一个自己的copy
		$(fieldDiv).parent().append(oldFieldDivHtml);
		
	}
	/**
		获取表单模块数据
	*/
	function getFormModuleObj(){
		var moduleInfo=$('input[name="moduleInfo"]').val();
		var className=$('input[name="className"]').val();
		var tableName=$('input[name="tableName"]').val();
		//找到所有属性div
		var fieldDivArr= $("div[name='fieldDiv']");
		var listField=[];
		fieldDivArr.each(function(){
			debugger;
			var fieldInfo= $(this).find('input[name=fieldInfo]').val();
			var fieldName= $(this).find('input[name=fieldName]').val();
			var fieldType= $(this).find('input[name^=fieldType]:checked').val();
			var fieldObj={
				fieldName:fieldName,
				fieldType:fieldType,
				fieldInfo:fieldInfo
			}
			listField.push(fieldObj);
		});
		var moduleObj= {
			moduleInfo:moduleInfo,
			className:className,
			tableName:tableName,
			listField:listField
		};
		return moduleObj;
	}
	
	/**
		添加或修改数据
	*/
	function saveData(){
		//是否保存的操作==没有id=id的input则会保存
		var moduleObj=getFormModuleObj();
		//alert(JSON.stringify(moduleObj));
		$.ajax({
			   type: "POST",
			   url: '${pageContext.request.contextPath}/genCode/genModuleCode1',
			   data: JSON.stringify(moduleObj),
			   contentType:'application/json',
			   dataType:'json',
			   success: function(jsonObj){
				    if (jsonObj.status == 200) {
				    	Zxx.notice("生成模块代码成功！");
					} else {
						Zxx.notice("生成模块代码失败！原因是：" + jsonObj.msg);
					} 
			   }
			});
	}
	/**
	  
    */
</script>

</html>